package org.megha.blog.example.selenium;

// imports all static objects (methods/classes/fields) from Assert class
// in this case, we're just using assertTrue() from Assert class
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

/**
 * A selenium based junit4 test to search for "google" on yahoo.com
 * 
 * @author megha birmiwal
 */
public class SeleniumYahooTest {

	static Selenium selenium;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.yahoo.com");
		selenium.start();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		selenium.stop();
	}

	@Before
	public void setUp() throws Exception {
		selenium.open("/");
	}

	@After
	public void tearDown() throws Exception {
		selenium.close();
	}

	@Test
	public void test() {
		selenium.waitForPageToLoad("3000");
		assertTrue("could not find id=xxx (for search textbox)", selenium.isElementPresent("name=p"));
		selenium.type("name=p", "Google");
		selenium.click("id=search-submit");
		selenium.waitForPageToLoad("3000");
		assertTrue("Search not giving correct results!", selenium.isTextPresent("www.google.com"));
	}
}
